//
//  XLProfileTextCellModel.m
//  Mine
//
//  Created by 于鹏 on 2017/8/30.
//  Copyright © 2017年 YIVIEW. All rights reserved.
//

#import "XLProfileTextCellModel.h"
//#import <XLBaseModule/XLAppMacro.h>
//#import <BaseModule/NSString+XLSize.h>
#import "XLAppMacro.h"
#import "NSString+XLSize.h"
NSString *const XLProfileTextCellModelCellClass = @"XLProfileTextCell";
//#define XL_KCellTitleHeight 60 //只有一行文本默认高度
//#define XL_KCellDetailHeight 90 //有详情文本默认高度
#define XL_KCellTitleHeight 40 //只有一行文本默认高度
#define XL_KCellDetailHeight 60 //有详情文本默认高度
@interface XLProfileTextCellModel ()
/** 默认最小高度高度*/
@property (nonatomic, assign, readonly) CGFloat minimumHeight;

@end

@implementation XLProfileTextCellModel

- (instancetype)init {
    self = [super init];
    if (self) {
        self.cellClass = XLProfileTextCellModelCellClass;
        self.titleFont = kXLDefaultFontSize;
        self.titleColor = kXLTextColor_BLACK;
        self.detailFont = kXLDetailFontSize;
        self.detailColor = kXLTextColor_GRAY;

    }
    return self;
}
- (instancetype)initWithTitle:(NSString *)title detailText:(NSString *)detailText {
    self = [self init];
    if (self) {
        self.title = title;
        self.detailText = detailText;
        self.titleFont = kXLDefaultFontSize;
        self.titleColor = kXLTextColor_BLACK;
        self.detailFont = kXLDetailFontSize;
        self.detailColor = kXLTextColor_GRAY;
    }
    return self;
}

+ (instancetype)modelWithTitle:(NSString *)title detailText:(NSString *)detailText {
    return [[self alloc]initWithTitle:title detailText:detailText];
}
#pragma mark - Setter
- (void)setDetailText:(NSString *)detailText {
    _detailText = detailText;
    if (detailText.length > 0) {
        self.cellHeightType = XLPCellHeightTypeDetail;
    }
    [self heightSizeWithText:detailText];
}

- (void)setCellHeightType:(XLPCellHeightType)cellHeightType {
    _cellHeightType = cellHeightType;
    switch (cellHeightType) {
        case XLPCellHeightTypeTitle:
            self.cellHeight = XL_KCellTitleHeight;
            break;
        case XLPCellHeightTypeDetail:
            break;
        default:
            break;
    }
}


#pragma mark - Getter
- (CGFloat)minimumHeight {
    CGFloat result;
    {
        switch (self.cellHeightType) {
            case XLPCellHeightTypeTitle:
                result = XL_KCellTitleHeight;
                break;
            case XLPCellHeightTypeDetail:
                result = XL_KCellDetailHeight;
                break;
            default:
                break;
        }
    }
    return result;
}

- (void)heightSizeWithText:(NSString *)text {
    self.cellHeight = 0.0f;
    CGFloat height = 0;
    CGFloat leftX = XLP_kTextLeftNormalMargin;
    height = [text xl_heightWithFont:self.detailFont constrainedToWidth:KScreenWidth-2*leftX];
    CGFloat titleHeight = 20;
    CGFloat totalHeight = height + XLP_kLabelMargin*2 + titleHeight;
    if (totalHeight < self.minimumHeight || text.length == 0) {
        self.cellHeight = [self minimumHeight];
    } else {
//        self.cellHeight = height + [self minimumHeight];
        self.cellHeight = totalHeight;

    }
}

@end
